﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HNCDataSetTableAdapters;

public partial class Admin_Home_Info : System.Web.UI.Page
{
    InfoTableAdapter info = new InfoTableAdapter();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadInfo(1);
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string image = "";
        if (FileUpload1.FileName.Trim() != "" && FileUpload1.FileName != null)
        {
            string[] arr = FileUpload1.FileName.Split('.');
            string ext = arr[arr.Length - 1];

            for (int j = 0; j < arr.Length - 1; j++)
            {
                image = image + arr[j];
            }

            image = image + FuntionHNC.ThemChuoiDatetime();
            image = image + "." + arr[arr.Length - 1];

            image = image.Replace('%', '-');

            String filePath = "~/UserUploads/Banner/" + image;
            FileUpload1.PostedFile.SaveAs(MapPath(filePath));
        }
        else
        {
            DataTable dt = info.GetInfoByID(1);
            image = dt.Rows[0]["Image"].ToString();
        }

        if (FuntionHNC.CheckFileType(image))
        {
            int i = info.Update(txtTitle.Text, txtTitle.Text, txtDescription.Text, txtDescription.Text, null, null, image, txtAddress.Text, txtWebsite.Text, txtPhone.Text, txtEmail.Text, txtYahoo.Text, txtYahoo1.Text, null, null, null, null, null, null, null, null, txtTenYahoo.Text, txtTenYahoo1.Text, txtMap.Text, null, null, null, true, 1);
            if (i > 0)
            {
                LoadInfo(1);
                lblMsg.Text = "Cập nhật thành công !";
            }
            else
            {
                lblMsg.Text = "Cập nhật thất bại !";
            }
        }
        else
        {
            lblMsg.Text = "Định dạng ảnh phải là *.png, *.jpg !";
        }
    }

    public void LoadInfo(int id)
    {
        DataTable dt = info.GetInfoByID(id);
        txtImage.ImageUrl = "~/UserUploads/Banner/" + dt.Rows[0]["Image"].ToString();
        txtTitle.Text = dt.Rows[0]["Title"].ToString();
        txtDescription.Text = dt.Rows[0]["Description"].ToString();
        txtAddress.Text = dt.Rows[0]["Address"].ToString();
        txtPhone.Text = dt.Rows[0]["Phone"].ToString();
        txtEmail.Text = dt.Rows[0]["Email"].ToString();
        txtWebsite.Text = dt.Rows[0]["Address_en"].ToString();
        txtYahoo.Text = dt.Rows[0]["Yahoo"].ToString();
        txtYahoo1.Text = dt.Rows[0]["Yahoo1"].ToString();
        //txtTenYahoo.Text = dt.Rows[0]["Facebook"].ToString();
        //txtPlus.Text = dt.Rows[0]["GooglePlus"].ToString();
        txtTenYahoo1.Text = dt.Rows[0]["GooglePlus2"].ToString();
        txtTenYahoo.Text = dt.Rows[0]["GooglePlus1"].ToString();
        txtMap.Text = dt.Rows[0]["GoogleMap"].ToString();
    }
}